import java.util.Stack;

public class FindPeakElement {

    public int findPeakElement1(int[] nums) {
        Stack<Integer> stack = new Stack<>();
        for (int i = 0; i < nums.length; i++) {
            if (!stack.isEmpty() && nums[i] < nums[stack.peek()]) {
                return stack.pop();
            }
            stack.push(i);
        }
        return stack.pop();
    }

    public int findPeakElement(int[] nums) {
        int l = 0;
        int r = nums.length - 1;
        while (l < r) {
            int mid = l + (r - l) / 2;
            if (nums[mid] < nums[mid + 1]) {
                l = mid + 1;
            } else {
                if (mid == 0 || nums[mid - 1] < nums[mid]) {
                    return mid;
                } else {
                    r = mid;
                }
            }
        }
        return l;
    }
}
